package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BaseOrganization;
import com.ruoyi.base.service.IBaseOrganizationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合作单位Controller
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/base/organization")
public class BaseOrganizationController extends BaseController
{
    @Autowired
    private IBaseOrganizationService baseOrganizationService;

    /**
     * 查询合作单位列表
     */
    @GetMapping("/select")
    public AjaxResult select(BaseOrganization baseOrganization)
    {
        List<BaseOrganization> list = baseOrganizationService.selectBaseOrganizationList(baseOrganization);
        return success(list);
    }

    /**
     * 查询合作单位列表
     */
    @PreAuthorize("@ss.hasPermi('base:organization:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseOrganization baseOrganization)
    {
        startPage();
        List<BaseOrganization> list = baseOrganizationService.selectBaseOrganizationList(baseOrganization);
        return getDataTable(list);
    }

    /**
     * 导出合作单位列表
     */
    @PreAuthorize("@ss.hasPermi('base:organization:export')")
    @Log(title = "合作单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseOrganization baseOrganization)
    {
        List<BaseOrganization> list = baseOrganizationService.selectBaseOrganizationList(baseOrganization);
        ExcelUtil<BaseOrganization> util = new ExcelUtil<BaseOrganization>(BaseOrganization.class);
        util.exportExcel(response, list, "合作单位数据");
    }

    /**
     * 获取合作单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:organization:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseOrganizationService.selectBaseOrganizationById(id));
    }

    /**
     * 新增合作单位
     */
    @PreAuthorize("@ss.hasPermi('base:organization:add')")
    @Log(title = "合作单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseOrganization baseOrganization)
    {
        return toAjax(baseOrganizationService.insertBaseOrganization(baseOrganization));
    }

    /**
     * 修改合作单位
     */
    @PreAuthorize("@ss.hasPermi('base:organization:edit')")
    @Log(title = "合作单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseOrganization baseOrganization)
    {
        return toAjax(baseOrganizationService.updateBaseOrganization(baseOrganization));
    }

    /**
     * 删除合作单位
     */
    @PreAuthorize("@ss.hasPermi('base:organization:remove')")
    @Log(title = "合作单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseOrganizationService.deleteBaseOrganizationByIds(ids));
    }

    @Log(title = "合作单位管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:organization:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseOrganization> util = new ExcelUtil<BaseOrganization>(BaseOrganization.class);
        List<BaseOrganization> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = baseOrganizationService.importData(dataList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BaseOrganization> util = new ExcelUtil<BaseOrganization>(BaseOrganization.class);
        util.importTemplateExcel(response, "合作单位");
    }
}
